package com.leyou.item.web;

import com.leyou.item.pojo.Brand;
import com.leyou.item.service.CategoryService;
import com.leyou.common.exception.LyException;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父分类id查询商品子分类
     *
     * @param pid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryListByPid(@RequestParam("pid") Long pid) throws LyException {

        //return ResponseEntity.status(HttpStatus.OK).body(null);
    //    推荐这样写：
        return  ResponseEntity.ok(categoryService.queryCategoryListByPid(pid));

    }

    /**
     * 根据品牌id查询分类
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryCategoryByBrandId(@PathVariable(name = "bid") Long bid){

        List<Category> categories = categoryService.queryCategoryListByBid(bid);
        return  ResponseEntity.ok(categories);
    }

    /**
     * 根据id集合，查询商品分类
     * @param ids
     * @return
     */
    @GetMapping("/list/ids")
    public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){

        return ResponseEntity.ok(categoryService.queryByIds(ids));
    }



}
